﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
#if AnimaRig
using UnityEngine.Animations.Rigging;

#endif

namespace UFrame
{
    public class IKShortWindow : EditorWindow
    {
        [MenuItem("Plateface/Animation/IK界面")]
        public static void Open()
        {
            var w = CreateWindow<IKShortWindow>("IK动画界面");
        }
        
        private static SerConstraint sc = null;

        public void OnGUI()
        {
            if (RecordTransform.memoryRecord != null)
            {
                GUILayout.Label("RecordTrans整体复制: " + RecordTransform.memoryRecord.name);
            }
            if (sc != null)
            {
                GUILayout.Label("constraint复制: " + sc.name);
            }

            var h = GUILayout.Height(30);
            if (GUILayout.Button("复制旋转节点", h))
            {
                RecordTransform.Record();
            }

            if (GUILayout.Button("粘贴节点", h))
            {
                RecordTransform.Paste();
            }

            if (GUILayout.Button("Rig Build", h))
            {
                var go = Selection.activeGameObject;
#if AnimaRig
                var build = go.GetComponent<RigBuilder>();
                if (build == null)
                    build = go.GetComponentInParent<RigBuilder>();

                if (build != null)
                {
                    Debug.Log("重新Builder");
                    build.Clear();
                    build.Build();
                }
#endif
            }

            if (GUILayout.Button("复制当前节点的Constraint信息",h))
            {
#if AnimaRig
                var go = Selection.activeGameObject;
                if (go == null)
                    return;

                sc = new SerConstraint();
                var m1 = go.GetComponent<MultiPositionConstraint>();
                if (m1 != null)
                {
                    var d = m1.data;
                    sc.positionData = d;
                }
                var m2 = go.GetComponent<MultiParentConstraint>();
                if (m2 != null)
                {
                    var d = m2.data;
                    sc.parentData = d;
                }

                sc.name = go.name;
                sc.position = go.transform.localPosition;
                sc.rotation = go.transform.localRotation;
                
            }

            if (GUILayout.Button("粘贴Constraint信息",h))
            {
                var go = Selection.activeGameObject;
                if (go == null)
                    return;

                if (sc == null){
                    Debug.Log("没有复制对象");
                    return;
                }

                var m1 = go.GetComponent<MultiPositionConstraint>();
                if (m1 != null)
                {
                    m1.data = sc.positionData;
                }
                var m2 = go.GetComponent<MultiParentConstraint>();
                if (m2 != null)
                {
                    m2.data = sc.parentData;
                }

                go.transform.localPosition = sc.position;
                go.transform.localRotation = sc.rotation;
#endif
            }


        }
    }

    public class SerConstraint
    {
#if AnimaRig
        public MultiPositionConstraintData positionData;
        public MultiParentConstraintData parentData;
#endif
        public Vector3 position;
        public Quaternion rotation;

        public string name;
        // public MultiPositionConstraint position;
        // public MultiParentConstraint parent;
    }

    // public class MultiPositionConstraintData
    // {
    //     public GameObject constraint;
    //     public List<GameObject> sourceObjects;
    //     public Vector3 offset;
    // }

    // public class MultiParentConstraintData
    // {
    //     public GameObject constraint;
    //     public List<GameObject> sourceObjects;
    //     public bool pX;
    //     public bool pY;
    //     public bool pZ;
    //     public bool rX;
    //     public bool rY;
    //     public bool rZ;
    // }


}